package com.smileus.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@SuppressWarnings("unchecked")
public class JSONUtil {
	/**
	 * toEntity:将对象转换为指定类型的实体
	 * 
	 * @param <T>
	 * @param source
	 * @param targetClass
	 * @return T
	 * @throws
	 */
	public static <T> T toEntity(Object source, Class<T> targetClass) {
		if (source == null || "null".equals(source.toString())) {
			return null;
		}
		Class clazz = source.getClass();
		if (targetClass == clazz) {
			return (T) source;
		}
		if (clazz == String.class) {
			if (targetClass == JSONObject.class) {
				return (T) JSONObject.parseObject((String) source);
			}
		} else {
			return JSONObject.parseObject((String) source, targetClass);
		}

		// JSONObject.parseObject暂不支持object，待温绍增加相应API
		String jsonString = JSON.toJSONString(source);
		return JSONObject.parseObject(jsonString, targetClass);
	}

	/**
	 * toEntity:将对象转换为装有指定类型实体的List
	 * 
	 * @param <T>
	 * @param source
	 * @param targetClass
	 * @return T
	 * @throws
	 */
	public static <T> List<T> toList(Object source, Class<T> targetClass) {
		ArrayList<T> list = new ArrayList<T>();
		if (source == null || "".equals(source.toString())) {
			return list;
		}
		if (source.getClass() == String.class) {
			return JSONArray.parseArray((String) source, targetClass);
		}
		if (source.getClass() == JSONArray.class) {
			JSONArray array = (JSONArray) source;
			for (int i = 0; i < array.size(); i++) {
				System.out.println(array.get(i));
			}
		}
		// 待温绍增加相应API
		String jsonString = JSON.toJSONString(source);
		return JSONArray.parseArray(jsonString, targetClass);
	}

	/**
	 * * @see java.lang.Boolean#TYPE
	 * 
	 * @see java.lang.Character#TYPE
	 * @see java.lang.Byte#TYPE
	 * @see java.lang.Short#TYPE
	 * @see java.lang.Integer#TYPE
	 * @see java.lang.Long#TYPE
	 * @see java.lang.Float#TYPE
	 * @see java.lang.Double#TYPE
	 * @see java.lang.Void#TYPE
	 * */
	static final Set<Class> simpleTypeSet = new HashSet<Class>() {
		private static final long serialVersionUID = 1L;
		{
			add(Boolean.class);
			add(Character.class);
			add(Byte.class);
			add(Short.class);
			add(Integer.class);
			add(Long.class);
			add(Float.class);
			add(Date.class);
			add(java.sql.Date.class);
			add(String.class);
			add(StringBuffer.class);
			add(StringBuilder.class);
			add(BigDecimal.class);
			add(BigInteger.class);
			add(Class.class);
		}
	};

	public static boolean isSimpleType(Class clazz) {
		return simpleTypeSet.contains(clazz);
	}

	public static <T> T toTarget(String source, Class<T> clazz) {
		if (isSimpleType(clazz)) {
			Parser parser = Parser.PARSER_MAP.get(clazz);
			if (parser == null) {
				throw new RuntimeException("暂不支持该类型的字符串值转换为" + clazz.getSimpleName());
			}
			Object result = parser.prase(source);
			return (T) result;
		}
		// 非简单类型的数据，按指定clazz转化

		return (T) JSONObject.parseObject(source, clazz);
	}

	public static Object toObject(String source, String type) {
		Class clazz = null;
		try {
			clazz = Class.forName(type);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		if (isSimpleType(clazz)) {
			Parser parser = Parser.PARSER_MAP.get(clazz);
			if (parser == null) {
				throw new RuntimeException("暂不支持该类型的字符串值转换为" + clazz.getSimpleName());
			}
			Object result = parser.prase(source);
			return result;
		}
		// 非简单类型的数据，按指定clazz转化
		return JSONObject.parseObject(source, clazz);
	}
}
